package com.tutorialspoint.test.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import com.tutorialspoint.test.model.User;
import com.tutorialspoint.test.view.LoginBean;

@ManagedBean
@RequestScoped
public class LoginService {
	
	HashMap<String, String[]> users = new HashMap<String, String[]>();
	
	@ManagedProperty(value = "#{loginBean}")
	private LoginBean loginBean;
	
	User user = new User();
	List<User> userList = new ArrayList<User>();
	
	@PostConstruct
	public void init() {
		String[] user1 = { "albert", "fernandes", "Chennai", "TN", "IN" };
		String[] user2 = { "shubasis", "sarangi", "bhubaneshwar", "OD", "IN" };
		String[] user3 = { "praveen", "kumar", "kochi", "Kerala", "IN" };
		String[] user4 = { "Vinoth", "Kumar", "Ambur", "TN", "IN" };
		String[] user5 = { "Shiva", "kumar", "madurai", "TN", "IN" };
		String[] user6 = { "Mani", "Gandan", "adyar", "TN", "IN" };
		String[] user7 = { "Subbu", "Ram", "Madurai", "TN", "IN" };
		String[] user8 = { "Anitha", "P", "Trichy", "TN", "IN" };
		String[] user9 = { "Shwetha", "Ji", "Chennai", "TN", "IN" };
		String[] user10 = { "Surya", "Kumar", "Chennai", "TN", "IN" };
		users.put("Albert", user1);
		users.put("Subhasis", user2);
		users.put("Praveen", user3);
		users.put("Vinoth", user4);
		users.put("Shiva", user5);
		users.put("Mani", user6);
		users.put("Subbu", user7);
		users.put("Anitha", user8);
		users.put("Shwetha", user9);
		users.put("Surya", user10);
		
		user = getUserInfo(loginBean.getUname());
		userList = getListOfUsers();
	}

	public LoginBean getLoginBean() {
		return loginBean;
	}

	public void setLoginBean(LoginBean loginBean) {
		this.loginBean = loginBean;
	}

	private User getUserInfo(String userName) {
		User user = new User();
		String[] userInfo = users.get(userName);
		user.setUserName(userName);
		user.setFirstName(userInfo[0]);
		user.setLastName(userInfo[1]);
		user.setCity(userInfo[2]);
		user.setState(userInfo[3]);
		user.setCountry(userInfo[4]);
		return user;
	}

	private List<User> getListOfUsers() {
		String[] userNames = { "Albert","Subhasis","Praveen","Vinoth","Shiva","Mani","Subbu","Anitha","Shwetha","Surya"};
		
		for (int i=0; i < userNames.length; i++){
			User user = new User();
			String[] userInfo = users.get(userNames[i]);
			user.setFirstName(userInfo[0]);
			user.setLastName(userInfo[1]);
			user.setCity(userInfo[2]);
			user.setState(userInfo[3]);
			user.setCountry(userInfo[4]);
			user.setPassWord("");
			user.setUserName(userNames[i]);
			userList.add(user);
		}
		
		return userList;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public List<User> getUserList() {
		return userList;
	}

}
